package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private RecommendUserApi recommendUserApi;

    //相互喜欢
    @Override
    public long queryEachLoveCount(long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long eachLoveCount = mongoTemplate.count(query, Friend.class);
        return eachLoveCount;
    }


    //我喜欢
    @Override
    public long queryLoveCount(long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long loveCount = mongoTemplate.count(query, UserLike.class);
        return loveCount;
    }

    //粉丝数
    @Override
    public long queryFansCount(long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long fansCount = mongoTemplate.count(query, UserLike.class);
        return fansCount;
    }

    //朋友
    @Override
    public PageResult queryEachLove(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page-1)*pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, Friend.class);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(friendList!=null){
            for (Friend friend : friendList) {
                Map<String,Object> map = new HashMap<>();
                map.put("userId",friend.getFriendId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId, friend.getFriendId());
                map.put("score",score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page,pagesize,(int)counts,mapList);
        return pageResult;
    }


    //我喜欢
    @Override
    public PageResult queryLove(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page-1)*pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(userLikeList!=null){
            for (UserLike userLike : userLikeList) {
                Map<String,Object> map = new HashMap<>();
                map.put("userId",userLike.getLikeUserId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId, userLike.getLikeUserId());
                map.put("score",score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page,pagesize,(int)counts,mapList);
        return pageResult;
    }

    //我的粉丝
    @Override
    public PageResult queryFans(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.skip((page-1)*pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(userLikeList!=null){
            for (UserLike userLike : userLikeList) {
                Map<String,Object> map = new HashMap<>();
                map.put("userId",userLike.getUserId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId,userLike.getUserId());
                map.put("score",score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page,pagesize,(int)counts,mapList);
        return pageResult;
    }


    //我的访客
    @Override
    public PageResult queryVisitors(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page-1)*pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, Visitors.class);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(visitorsList!=null){
            for (Visitors visitors : visitorsList) {
                Map<String,Object> map = new HashMap<>();
                map.put("userId",visitors.getVisitorUserId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId,visitors.getVisitorUserId());
                map.put("score",score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page,pagesize,(int)counts,mapList);
        return pageResult;
    }

    //删除粉丝的数据
    @Override
    public void delete(long userId, Long fansId) {
        Query query = new Query(Criteria.where("userId").is(fansId).and("likeUserId").is(userId));
        mongoTemplate.remove(query,UserLike.class);
    }

    @Override
    public Boolean alreadyLove(long uid, long myid) {
        Query query=new Query(Criteria.where("userId").is(myid).and("likeUserId").is(uid));
        UserLike one = mongoTemplate.findOne(query, UserLike.class);
        if(one!=null){
            return true;
        }
        return false;
    }


}
